package net.ib.mtalk.ui.setting.user;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.AccountInfo;
import net.ib.mntalk.protocol.Common.Calendar;
import net.ib.mntalk.protocol.Common.EmailAccountInfo;
import net.ib.mntalk.protocol.Common.FacebookAccountInfo;
import net.ib.mntalk.protocol.Common.FileKey;
import net.ib.mntalk.protocol.Common.MnTalkAccountInfo;
import net.ib.mntalk.protocol.Common.PhoneAccountInfo;
import net.ib.mntalk.protocol.Common.UserProfile;
import net.ib.mntalk.protocol.Session.GetUserAccountsRes;
import net.ib.mtalk.R;
import net.ib.mtalk.base.BaseActivity;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.task.ProfileDownloadTask;
import net.ib.mtalk.network.task.ProfileUploadTask;
import net.ib.mtalk.ui.attachment.camera.CameraActivity;
import net.ib.mtalk.ui.attachment.emoticon.RichString;
import net.ib.mtalk.ui.start.AccountPhoneActivity;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.LockActivity;
import net.ib.mtalk.util.MTalkLogWriter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class UserActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener{
	
	public static final int GET_ACCOUNTS_COUNT = 50;
	
	private Context mContext;
	private UserController mUiController;
	
	private Button mMakeIdBtn;
	private Button mAddNumberBtn;
	private Button mAddAccountBtn;
	private EditText mEditName;
	private EditText mEditStatus;
	private EditText mEditBirthday;
	private CheckBox mCheckLock;
	private CheckBox mCheckConfirm;
	private ImageView mProfileImageBtn;
	private LinearLayout mPhoneContainer;
	private LinearLayout mAccountContainer;
	private Button mEditImageBtn;
	private ImageButton mDeleteBirthBtn;
	
	private ArrayList<String> mListString = new ArrayList<String>();
	
	private String text_userName;
	private String text_userStatus;
	
	private final int LIST_DIALOG = 0x01;
	private final int BIRTHDAY_DIALOG = 0x02;
	
	private final int PICK_FROM_ALBUM = 0x10;
	private final int PICK_FROM_CAMERA = 0x11;
	private final int PICK_CROP_IMAGE = 0x13;
	private final int MAKE_ID = 0x14;
	
	private int mCreatedDialogID;
	private Bitmap mProfileImage;
	
	private int mYear = -1;
	private int mMonth = -1;
	private int mDay = -1;
	
	private Uri mCaptureImageUri;
	private File mImageFile;
	
	//private EmoticonDialog emoticonDialog = new EmoticonDialog();
	
	private Map<CharSequence, CharSequence> mCountryMap = new HashMap<CharSequence, CharSequence>();
	
	private ProgressDialog mProgressDialog;
	
	private ArrayList<String> mCountryCodeArray = new ArrayList<String>();
	private ArrayList<String> mPhoneNumberArray = new ArrayList<String>();
	
	private ArrayList<String> mFacebookAccountArray = new ArrayList<String>();

	private ArrayList<String> mEmailAccountArray = new ArrayList<String>();
	
	private boolean bImageOrIdChanged = false;
	private boolean bInitLockValue = false;
	private boolean bInitReadMarkValue = false;
	private String mInitName = "";
	private String mInitStatus = "";
	private int mInitYear = -1;
	private int mInitMonth = -1;
	private int mInitDay = -1;
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case HandlerMsgConst.HANDLER_UNKNOWN_ERROR:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_UNKNOWN_ERROR", Toast.LENGTH_SHORT).show();
				finish();
				break;
			case HandlerMsgConst.HANDLER_GET_USER_PROFILE_SUCCESS:
				mProgressDialog.hide();
				updateUserProfileInfo((UserProfile)msg.obj);
				Toast.makeText(mContext, "HANDLER_GET_USER_PROFILE_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_GET_USER_PROFILE_FAIL:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_GET_USER_PROFILE_FAIL", Toast.LENGTH_SHORT).show();
				finish();
				break;
			case HandlerMsgConst.HANDLER_SET_USER_PROFILE_SUCCESS:
				mProgressDialog.hide();
				Toast.makeText(mContext, R.string.save_complete_msg, Toast.LENGTH_SHORT).show();
				finish();
				break;
			case HandlerMsgConst.HANDLER_SET_USER_PROFILE_FAIL:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_SET_USER_PROFILE_FAIL", Toast.LENGTH_SHORT).show();
				finish();
				break;
			case HandlerMsgConst.HANDLER_GET_USER_ACCOUNT_SUCCESS:
				GetUserAccountsRes getUserAccountsRes = (GetUserAccountsRes) msg.obj;
				updateUserAccountInfo(getUserAccountsRes);
				break;
			case HandlerMsgConst.HANDLER_GET_USER_ACCOUNT_FAIL:
				Toast.makeText(mContext, "HANDLER_GET_USER_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_PROFILE_UPLOAD_SUCCESS:
				setUserProfile((FileKey) msg.obj);
				Toast.makeText(mContext, "HANDLER_PROFILE_UPLOAD_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_PROFILE_UPLOAD_FAIL:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_PROFILE_UPLOAD_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_SET_USER_PROFILE_IMAGE_SUCCESS:
				mProgressDialog.hide();
				mProfileImageBtn.setImageBitmap(mProfileImage);
				Toast.makeText(mContext, "HANDLER_SET_USER_PROFILE_IMAGE_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_SET_USER_PROFILE_IMAGE_FAIL:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_SET_USER_PROFILE_IMAGE_FAIL", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_PROFILE_DOWNLOAD_SUCCESS:
				mProgressDialog.hide();
				mProfileImageBtn.setImageBitmap(BitmapFactory.decodeFile(msg.obj.toString()));
				Toast.makeText(mContext, "HANDLER_PROFILE_DOWNLOAD_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_PROFILE_DOWNLOAD_FAIL:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_PROFILE_DOWNLOAD_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_UNREGISTER_ACCOUNT_SUCCESS:
				getUserAccount();
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_UNREGISTER_ACCOUNT_SUCCESS", Toast.LENGTH_SHORT).show();
				break;
			case HandlerMsgConst.HANDLER_UNREGISTER_ACCOUNT_FAIL:
				mProgressDialog.hide();
				Toast.makeText(mContext, "HANDLER_UNREGISTER_ACCOUNT_FAIL", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		
		mContext = this;
		mUiController = new UserController(this, mHandler);
		
		mListString.add(getString(R.string.setting_theme_from_capture));
		mListString.add(getString(R.string.setting_theme_from_album));
		mListString.add(getString(R.string.delete));
		
		initialView();
		setProfileImageUri();
		getUserProfile();
		
		CharSequence[] country = getResources().getTextArray(R.array.country);
		CharSequence[] countryCode = getResources().getTextArray(R.array.country_code);
		
		for(int i=0; i < country.length; ++i) 
			mCountryMap.put(country[i], countryCode[i]);
		
		Log.i("MTalk", new UUID(GpbSupport.msbOf(GlobalPreferences.getUserUUID()), GpbSupport.lsbOf(GlobalPreferences.getUserUUID())).toString() + "");
	}
	
	public void setProfileImageUri()
	{
		try {
			String name = new UUID(GpbSupport.msbOf(GlobalPreferences.getUserUUID()), GpbSupport.lsbOf(GlobalPreferences.getUserUUID())).toString();
			new File(MTalkConstant.MTALK_PATH+"/profile").mkdir(); 
			mImageFile = new File(MTalkConstant.MTALK_PATH + "/profile", name);
			mImageFile.createNewFile();
			mCaptureImageUri = Uri.fromFile(mImageFile);
		} catch (IOException e) {
			MTalkLogWriter.e(e);
		}
	}
	
	public void onResume()
	{
		super.onResume();
		getUserAccount();

		mCheckLock.setOnCheckedChangeListener(null);
		mCheckLock.setChecked(GlobalPreferences.getBooleanLockChk());
		mCheckLock.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent intent = new Intent(mContext, LockActivity.class);
				if(isChecked)
					intent.putExtra("type", "lock");
				else
					intent.putExtra("type", "unlock");
				startActivity(intent);
			}}
		);
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		if(mProgressDialog!=null)
			mProgressDialog.dismiss();
	}
	
	private void updateUserAccountInfo(GetUserAccountsRes getUserAccountsRes)
	{
		List<AccountInfo> accountInfoList = getUserAccountsRes.getAccountsList();
		mPhoneContainer.removeAllViews();
		mAccountContainer.removeAllViews();
		mCountryCodeArray.clear();
		mPhoneNumberArray.clear();
		mFacebookAccountArray.clear();
		mEmailAccountArray.clear();
		for( int i = 0 ; i < accountInfoList.size() ; i++ )
		{
			PhoneAccountInfo phoneAccountInfo = accountInfoList.get(i).getPhone();
			MnTalkAccountInfo mnTalkAccountInfo = accountInfoList.get(i).getMntalk();
			FacebookAccountInfo facebookAccountInfo =  accountInfoList.get(i).getFacebook();
			EmailAccountInfo emailAccountInfo =  accountInfoList.get(i).getEmail();
			if( phoneAccountInfo != null )
			{
				String code = phoneAccountInfo.getCountryCode();
				String number = phoneAccountInfo.getPhoneNumber();
				
				mCountryCodeArray.add(code);
				mPhoneNumberArray.add(number);
				
				StringBuilder sb = new StringBuilder().append(code).append(" ").append(Integer.parseInt(number));
				TextView tv = new TextView(mContext);
				tv.setText(sb.toString());
				mPhoneContainer.addView(tv);
			}
			
			if(mnTalkAccountInfo != null)
			{
				mMakeIdBtn.setText(mnTalkAccountInfo.getId());
			}
			
			if(facebookAccountInfo != null)
			{
				final String facebookId = facebookAccountInfo.getFacebookId();
				
				mFacebookAccountArray.add(facebookId);
				
				LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View v = inflater.inflate(R.layout.item_service_account, null);
				TextView tv = (TextView)v.findViewById(R.id.service_id);
				TextView summary = (TextView)v.findViewById(R.id.service_summary);
				ImageView icon = (ImageView)v.findViewById(R.id.service_icon);
				
				tv.setText(facebookId);
				summary.setText(getString(R.string.setting_profile_facebook_summary));
				icon.setBackgroundResource(R.drawable.facebook);
				
				Button btn = (Button)v.findViewById(R.id.service_delete);
				btn.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						mUiController.unRegisterAccount(facebookId, Common.Facebook);
						mProgressDialog.show();
					}
				});
				mAccountContainer.addView(v);
			}
			
			if(emailAccountInfo != null)
			{
				final String emailId = emailAccountInfo.getAddress();
				
				mEmailAccountArray.add(emailId);
				
				LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View v = inflater.inflate(R.layout.item_service_account, null);
				TextView tv = (TextView)v.findViewById(R.id.service_id);
				TextView summary = (TextView)v.findViewById(R.id.service_summary);
				ImageView icon = (ImageView)v.findViewById(R.id.service_icon);
				
				tv.setText(emailId);
				summary.setText(getString(R.string.setting_profile_email_summary));
				icon.setBackgroundResource(R.drawable.email_blue);
				
				Button btn = (Button)v.findViewById(R.id.service_delete);
				btn.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						mUiController.unRegisterAccount(emailId, Common.Email);
						mProgressDialog.show();
					}
				});
				mAccountContainer.addView(v);
			}
		}
	}
	
	private void updateUserProfileInfo(UserProfile userProfile) 
	{
		Calendar cld = GlobalPreferences.getUserBirthday();
		mEditName.setText(GlobalPreferences.getUserName());
		RichString.enrich(mEditName);
		mEditStatus.setText(GlobalPreferences.getUserStatus());
		RichString.enrich(mEditStatus);
		
		if(cld != null)
		{
			mYear = cld.getYear();
			mMonth = cld.getMonth();
			mDay = cld.getDay();
			updateBirthday();
		}
		else
		{
			mYear = -1;
			mMonth = -1;
			mDay = -1;
			updateBirthday();
		}
		
		FileKey fileKey = userProfile.getProfileImage();
		if(fileKey != null)
		{
			ProfileDownloadTask task = new ProfileDownloadTask(mContext, mHandler, GlobalPreferences.getUserUUID(), fileKey, null, -1);
			task.execute();
			mProgressDialog.show();
		}
		
		mInitName = GlobalPreferences.getUserName();
		mInitStatus = GlobalPreferences.getUserStatus();
		mInitYear = mYear;
		mInitMonth = mMonth;
		mInitDay = mDay;
	}
	
	/*public void onEmoticonClick(View view)
	{
		emoticonDialog.create(mContext, mEditStatus).show();
	}*/

	private void initialView() {
		mProgressDialog = new ProgressDialog(mContext);
		mProgressDialog.setMessage(getString(R.string.waiting));
		mProgressDialog.setCancelable(false);
		
		mEditName = (EditText) findViewById(R.id.edit_name);
		
		mEditStatus = (EditText) findViewById(R.id.edit_status);
		
		mEditBirthday = (EditText) findViewById(R.id.edit_birthday);
		mEditBirthday.setOnClickListener(this);
		
		mProfileImageBtn = (ImageView) findViewById(R.id.imgbtn_profile);
		
		mEditImageBtn = (Button) findViewById(R.id.imgbtn_edit);
		mEditImageBtn.setOnClickListener(this);
		
		mCheckLock = (CheckBox) findViewById(R.id.message_receive_lock_chk);
		bInitLockValue = GlobalPreferences.getBooleanLockChk();
		/*mCheckLock.setChecked(GlobalPreferences.getBooleanLockChk());
		mCheckLock.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent intent = new Intent(mContext, LockActivity.class);
				if(isChecked)
					intent.putExtra("type", "lock");
				else
					intent.putExtra("type", "unlock");
				startActivity(intent);
			}}
		);*/
		
		mCheckConfirm = (CheckBox) findViewById(R.id.message_receive_confirm_chk);
		bInitReadMarkValue = GlobalPreferences.getBooleanReceiveConfirmChk();
		mCheckConfirm.setChecked(GlobalPreferences.getBooleanReceiveConfirmChk());
		mCheckConfirm.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				GlobalPreferences.setBooleanReceiveConfirmChk(isChecked);
			}}
		);
		
		mMakeIdBtn = (Button) findViewById(R.id.btn_make_id);
		mMakeIdBtn.setOnClickListener(this);
		
		mAddNumberBtn = (Button) findViewById(R.id.btn_add_number);
		mAddNumberBtn.setOnClickListener(this);
		
		mPhoneContainer = (LinearLayout)findViewById(R.id.profile_phone_container);
		
		mAccountContainer = (LinearLayout)findViewById(R.id.profile_account_container);
		
		mAddAccountBtn = (Button)findViewById(R.id.btn_add_account);
		mAddAccountBtn.setOnClickListener(this);
		
		mDeleteBirthBtn = (ImageButton)findViewById(R.id.birth_delete);
		mDeleteBirthBtn.setOnClickListener(this);
	}
	
	/*public void onSaveClick(View v)
	{
		mProgressDialog.show();
		saveUserInfo();
	}*/
	
	public void onBackPressed()
	{
		super.onBackPressed();
		if(isChanged())
		{
			mProgressDialog.show();
			setUserProfile();
		}
		else
			finish();
	}
	
	private boolean isChanged()
	{
		if(bImageOrIdChanged)
			return true;
		
		if(bInitLockValue != GlobalPreferences.getBooleanLockChk())
			return true;
		
		if(bInitReadMarkValue != GlobalPreferences.getBooleanReceiveConfirmChk())
			return true;
		
		if(!mEditName.getText().toString().equals(mInitName))
			return true;
		
		if(!mEditStatus.getText().toString().equals(mInitStatus))
			return true;
		
		if(mInitYear != mYear || mInitMonth != mMonth || mInitDay != mDay)
			return true;
		
		return false;
	}

	private void getUserProfile(){
		mUiController.getUserProfile();
		mProgressDialog.show();
	}
	
	private void getUserAccount()
	{
		mUiController.getUserAccount();
	}
	
	private void setUserProfile() 
	{
		text_userName = (mEditName.getText().toString() == null ? "" : mEditName.getText().toString());
		text_userStatus = (mEditStatus.getText().toString() == null ? "" : mEditStatus.getText().toString());
		
		mUiController.setUserProfile(text_userName, text_userStatus, mYear, mMonth, mDay);
	}
	
	private void setUserProfile(FileKey fileKey) 
	{
		mUiController.setUserProfile(fileKey);
	}
	
	public void onClick(View v) 
	{
		if(v.equals(mEditImageBtn))
		{
			showDialog(LIST_DIALOG);
		}
		else if(v.equals(mEditBirthday))
		{
			showDialog(BIRTHDAY_DIALOG);
		}
		else if(v.equals(mMakeIdBtn))
		{
			Intent intent = new Intent(mContext, MakeIdActivity.class);
			if(!mMakeIdBtn.getText().toString().equals(getString(R.string.setting_profile_create_account)))
			{
				intent.putExtra("type", "modify");
				intent.putExtra("id", mMakeIdBtn.getText().toString());
			}
			else
				intent.putExtra("type", "make");
			startActivityForResult(intent, MAKE_ID);
		}
		else if(v.equals(mAddNumberBtn))
		{
			//showDialog(PHONE_DIALOG);
			Intent intent = new Intent(mContext, AccountPhoneActivity.class);
			intent.putExtra("redirect", true);
			intent.putStringArrayListExtra("code", mCountryCodeArray);
			intent.putStringArrayListExtra("number", mPhoneNumberArray);
			startActivity(intent);
		}
		else if(v.equals(mAddAccountBtn))
		{
			Intent intent = new Intent(mContext, AddServiceAccountActivity.class);
			intent.putStringArrayListExtra("facebook", mFacebookAccountArray);
			intent.putStringArrayListExtra("email", mEmailAccountArray);
			startActivity(intent);
		}
		else if(v.equals(mDeleteBirthBtn))
		{
			mEditBirthday.setText("");
			
			mYear = -1;
			mMonth = -1;
			mDay = -1;
		}
	}
	
	
	protected Dialog onCreateDialog(int id) {	
		Dialog dialog;
		mCreatedDialogID = id;
		switch(mCreatedDialogID) {
		case LIST_DIALOG:
			dialog = new AlertDialog.Builder(this)
					.setTitle(getString(R.string.setting_profile_select_image_msg))
					.setView(makeView())
					.setNegativeButton(R.string.close, new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							removeDialog(LIST_DIALOG);
						}
					}).create();
			dialog.setOnCancelListener(new OnCancelListener(){
				public void onCancel(DialogInterface dialog) {
					removeDialog(LIST_DIALOG);
				}});
					return dialog;
		case BIRTHDAY_DIALOG:
			if(mYear == -1)
				mYear = 1980;
			if(mMonth == -1)
				mMonth = 1;
			if(mDay == -1)
				mDay = 1;
	        
			dialog = new DatePickerDialog(mContext, mBirthdaySetListener, mYear, mMonth-1, mDay);
			dialog.setOnDismissListener(new OnDismissListener(){
				public void onDismiss(DialogInterface dialog) 
				{
					removeDialog(BIRTHDAY_DIALOG);
				}});
			dialog.setOnCancelListener(new OnCancelListener(){
				public void onCancel(DialogInterface dialog) {
					removeDialog(BIRTHDAY_DIALOG);
				}});
					return dialog;
		/*case MAKEID_DIALOG:
			dialog = new AlertDialog.Builder(this)
					.setTitle(getString(R.string.new_account))
					.setView(makeView())
					.setNegativeButton(R.string.registration, new OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							removeDialog(MAKEID_DIALOG);
						}
					}).create();
			dialog.setOnCancelListener(new OnCancelListener(){
				public void onCancel(DialogInterface dialog) {
					removeDialog(MAKEID_DIALOG);
				}});
					return dialog;*/
		default:
			break;
		}
		
		return super.onCreateDialog(id);
	}
	
	private DatePickerDialog.OnDateSetListener mBirthdaySetListener = 
		new DatePickerDialog.OnDateSetListener() 
		{
			public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) 
			{
				mYear = year;
				mMonth = monthOfYear+1;
				mDay = dayOfMonth;
				
				updateBirthday();
            }
        };
        
    private void updateBirthday()
    {
    	if(mYear > 0 && mMonth > 0 && mDay > 0)
    		mEditBirthday.setText(mYear + " / " + mMonth + " / " + mDay);
    	else
    		mEditBirthday.setText("");
    }
	
	private View makeView()
	{
		View returnView = null;
		ListView listView = null;
		LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		switch(mCreatedDialogID)
		{
		case LIST_DIALOG:
			listView = new ListView(this);
			listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			listView.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, mListString));
			listView.setCacheColorHint(0x00000000);
			listView.setOnItemClickListener(this);
			returnView = listView;
			break;
		case BIRTHDAY_DIALOG:
			listView = new ListView(this);
			listView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			listView.setAdapter(new ArrayAdapter<String>(mContext, android.R.layout.simple_list_item_1, mListString));
			listView.setCacheColorHint(0x00000000);
			listView.setOnItemClickListener(this);
			returnView = listView;
			break;
		/*case MAKEID_DIALOG:
			returnView = inflater.inflate(R.layout.privacy_makeid_dialog_body, null, false);
			break;*/
		default:
			break;
		}
		
		return returnView;
	}

	public void onItemClick(AdapterView<?> arg0, View v, int pos, long id) 
	{
		switch(mCreatedDialogID)
		{
		case LIST_DIALOG:
			if(pos == 0)
			{
				Intent i = new Intent(mContext, CameraActivity.class);
				startActivityForResult(i, PICK_FROM_CAMERA);
			}
			else if(pos == 1)
			{
				Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
				startActivityForResult(i, PICK_FROM_ALBUM);
			}
			else if(pos == 2)
			{
				FileKey fileKey = new FileKey();
				fileKey.setFileName("null");
				fileKey.setFileType(1);
				fileKey.setFileUuid(GlobalPreferences.getUserUUID());
				mProfileImage = BitmapFactory.decodeResource(getResources(), R.drawable.pic_default_big);
				mUiController.setUserProfile(fileKey);
				mProgressDialog.show();
				
				/*try {
					mProfileImage = BitmapFactory.decodeResource(getResources(), R.drawable.onetoone_pic);
					makeImageFile();
					
					ProfileUploadTask task = new ProfileUploadTask(mContext, mHandler, GlobalPreferences.getUserUUID(), mCaptureImageUri);
					task.execute();
					
					mProgressDialog.show();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}*/
			}
			
			removeDialog(LIST_DIALOG);
			break;
		default:
			break;
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if(requestCode == PICK_FROM_ALBUM || requestCode == PICK_FROM_CAMERA) 
		{
			if (resultCode == Activity.RESULT_OK) 
			{ 
				if(data.getData() != null)
				{
					Intent intent = new Intent("com.android.camera.action.CROP");
					intent.setData(data.getData());
					intent.putExtra("outputX", 200);
					intent.putExtra("outputY", 200);
					/*intent.putExtra("aspectX", 1);
					intent.putExtra("aspectY", 1);
					intent.putExtra("scale", true);*/
					intent.putExtra("return-data", true);
					startActivityForResult(intent, PICK_CROP_IMAGE);
				}
			} 
		}
		else if(requestCode == PICK_CROP_IMAGE)
		{
			if(resultCode == Activity.RESULT_OK)
			{
				try{
					Bundle extras = data.getExtras();
					mProfileImage = extras.getParcelable("data");
					
					makeImageFile();
					
					ProfileUploadTask task = new ProfileUploadTask(mContext, mHandler, GlobalPreferences.getUserUUID(), mCaptureImageUri);
					task.execute();
					
					mProgressDialog.show();
					bImageOrIdChanged = true;
				}catch(Exception e)
				{
					Log.e(MTalkConstant.TAG, "PICK_CROP_IMAGE", e);
					mProfileImage = BitmapFactory.decodeResource(getResources(), R.drawable.iu);
					mProfileImageBtn.setImageBitmap(mProfileImage);
				}
			}
		}
		else if(requestCode == MAKE_ID)
		{
			if(resultCode == Activity.RESULT_OK)
			{
				bImageOrIdChanged = true;
			}
		}
	}
	
	private void makeImageFile()
	{
		OutputStream out = null;
		        
	    try {
			out = new FileOutputStream(mImageFile);
			mProfileImage.compress(CompressFormat.PNG, 100, out);	
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
